package loppy.dynamics 
{
  import loppy.collision.Collision;
  import loppy.loppy_internal;
  import loppy.utils.InListNode;
  import loppy.utils.ObjectPool;
  
  public class Contact extends InListNode
  {
    use namespace loppy_internal;
    
    loppy_internal static var pool:ObjectPool = new ObjectPool(Contact);
    
    public var contactPoints:Vector.<ContactPoint> = new Vector.<ContactPoint>();
    
    public var type    :uint        = 0;
    public var edge1   :ContactEdge = null;
    public var edge2   :ContactEdge = null;
    public var shape1  :Shape       = null;
    public var shape2  :Shape       = null;
    
    public var isTouching :Boolean  = false;
    public var wasTouching:Boolean  = false;
    
    public function update():void
    {
      //recycle contact points
      for (var i:int = 0, len:int = contactPoints.length; i < len; ++i)
      {
        ContactPoint.pool.put(contactPoints[i].dispose());
      }
      contactPoints.length = 0;
      
      //check contact
      switch (type)
      {
        case Collision.CIRCLE_CIRCLE:
        {
          Collision.collideCircles(contactPoints, CircleShape(shape1), CircleShape(shape2));
          break;
        }
        case Collision.POLYGON_CIRCLE:
        {
          Collision.collidePolyconCircle(contactPoints, PolygonShape(shape1), CircleShape(shape2));
          break;
        }
      }
      
      //update touching status
      wasTouching = isTouching;
      isTouching = (contactPoints.length > 0);
    }
    
    public function dispose():Contact
    {
      edge1  = edge2  = null;
      shape1 = shape2 = null;
      return this;
    }
  }
}
